/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.utils;


import com.jspx.boot.environment.Environment;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: chenYuan
 * Date: 2004-7-19
 * Time: 21:48:44
 */
public class MapUtil
{
    private MapUtil()
    {

    }

    public static boolean isNotEmpty(Map map)
    {
        return !(map == null || map.isEmpty());
    }


    public static List toList(Map map)
    {
        if (map == null || map.isEmpty()) return new ArrayList();
        List<Object> list = new ArrayList<Object>();
        for (Object o : map.values())
        {
            list.add(o);
        }
        return list;
    }


    /*
    吧哈西表中对象用字符串输出
    */
    public static String toString(Map map, String fen)
    {
        if (map == null || map.isEmpty()) return StringUtil.empty;
        if (StringUtil.isNULL(fen)) return StringUtil.empty;
        StringBuilder result = new StringBuilder();
        for (Object key : map.keySet())
        {
            result.append(key).append(":").append(map.get(key)).append(fen);
        }
        if (result.toString().endsWith(fen)) result.setLength(result.length()-fen.length());
        return result.toString();
    }

    public static String toString(Map map)
    {
        return toString(map, "/");
    }

    /**
     * 转换为
     * @param map
     * @return
     */
    public static String toQueryString(Map map)
    {
        if (map == null || map.isEmpty()) return StringUtil.empty;
        StringBuilder result = new StringBuilder();
        for (Object key : map.keySet())
        {
            String value = (String)map.get(key);
            result.append(key).append("=").append(URLUtil.getURLEncoder(value, Environment.defaultEncode)).append("&");
        }
        if (result.toString().endsWith("&")) result.setLength(result.length()-1);
        return result.toString();
    }


    @SuppressWarnings("unchecked")
    public static <String extends Comparable, V > Map sortByKey(Map<?,V> map) {
        List<Map.Entry<?, V>> list = new LinkedList<Map.Entry<?, V>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<?, V>>() {
            @Override
            public int compare(Map.Entry<?, V> o1, Map.Entry<?, V> o2) {
                return ((String)o1.getKey()).compareTo(o2.getKey());
            }
        });
        Map<String, V> result = new LinkedHashMap();
        for (Map.Entry<?, V> entry : list) {
            result.put((String)entry.getKey(), entry.getValue());
        }
        return result;
    }

    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

    public static String phpKSort(Map<String, Object> map)  {

        String sb = "";
        String[] key = new String[map.size()];
        int index = 0;
        for (String k : map.keySet()) {
            key[index] = k;
            index++;
        }
        Arrays.sort(key);
        for (String s : key) {
            sb += s + "=" + map.get(s) + "&";
        }
        sb = sb.substring(0, sb.length() - 1);
        // 将得到的字符串进行处理得到目标格式的字符串
        try {
            sb = URLEncoder.encode(sb, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }// 使用常见的UTF-8编码
        String str = sb.replace("%3D", "=").replace("%26", "&");
        System.out.println("-----------str=" + str);
        return str;
    }




}